<template>
	<div class="content">
	    <div class="cloud-header">
	      <h2 class="r-title-h2">会员管理</h2>
	    </div>
		<div class="cloud-body">
			<div class="r-member-level">
				<div class="r-member-title">会员等级配置</div>
				<div class="r-level-item" v-for="item,index in level" :key="index"><input type="text" v-model="item.name" /> 会员。升级条件：消费满<NumberInput :precision="2" v-model="item.upgrade" v-width="200" />元</div>
			</div>
			<Button color="primary" class="r-member-save"  @click="intSubmit"><i class="iconfont icon-save r-submit-icon"></i>保存</Button>
		</div>
	</div>
</template>
<script type="text/javascript">
	export default{
		name:"",
		data(){
			return {
				name:'user_level',
				_id:'',

				level:[{
					name:"一星",
					value:1,
					upgrade:0,
				},
				{
					name:"两星",
					value:2,
					upgrade:100,
				},
				{
					name:"三星",
					value:3,
					upgrade:500,
				},
				{
					name:"四星",
					value:4,
					upgrade:1500,
				},
				{
					name:"五星",
					value:5,
					upgrade:4000,
				}]
			}
		},
		mounted(){
			this.getLevel();
		},
		methods:{

			/**
			 * 获取id
			 * @return {[type]} [description]
			 */
			getLevel(){
				this.$cloud.deploy().get(this.name).then(res =>{
					if(res){
						this._id = res._id;
						if(res.data.data.length > 0){
							this.level = res.data.data;
						}
					}
				})
			},



			/**
			 * 设置会员等级系统
			 * @return {[type]} [description]
			 */
			intSubmit(){
				this.reg = false;
				this.level.forEach(item => {
					for(let key in item){
						if(item[key] == '' && item[key] != 0){
							console.log(item[key])
							this.reg = true;
						}
					}
				})
				if(this.reg){
					this.$Message.warn("请设置完整等级体系！");
					return false;
				}

				this.$cloud.deploy().edit(this._id,this.level).then(data =>{
					if(data){
						this.$Message.success('等级设置成功！')
					}
				}).catch(err =>{
					this.$Message.error("网络异常，请稍后重试！");
				})
			}
		}
	}
</script>
<style type="text/css" lang="less">
.r-member-level{
	border:solid 1px #dfdfdf;
	padding: 30px;
	position: relative;
	.r-level-item{
		display: flex;
		align-items: center;
		justify-content: flex-start;
		margin: 20px 0;
		input,.h-numberinput-show{
			margin: 0px 10px;
			text-align: center;
		}
	}
}
.r-member-save{
	margin-top: 20px;
}
.r-member-title{
	padding: 10px 20px;
	background-color: #fff;
	text-align: center;
	line-height: 20px;
	position: absolute;
	left:40px;
	top: -20px;
}
</style>